/*
 * Copyright 2012 Jonathan St-Michel <jonathan@abiwebz.net>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.abiwebz.office.rm.database;

import java.util.ArrayList;

/**
 * Register the Drivers for the Database access. Only the selected one in
 * the configuration file are used for the Application runtime. Other are used
 * for offline connection or change of domain.
 * 
 * @author Jonathan St-Michel <jonathan@abiwebz.net>
 */
public class DatabaseDrivers extends ArrayList<DatabaseDriver> {
    
    private static DatabaseDrivers instance; //Singleton static instance
    
    /**
     * Create new instance of DatabaseDrivers that will be used to get
     * the drivers.
     * 
     * @return New instance of DatabaseDrivers
     */
    public static DatabaseDrivers create(){
        DatabaseDrivers.instance = new DatabaseDrivers();
        return DatabaseDrivers.instance;
    }
    
    /**
     * Return default instance of DatabaseDrivers class. If no instance 
     * exist because DatabaseDrivers.create() was not called before, the function
     * will try to create an instance before returning it.
     * 
     * @return Default DatabaseDrivers instance
     */
    public static DatabaseDrivers get(){
        if(DatabaseDrivers.instance == null){ //No instance is created
            return DatabaseDrivers.create();
        }
        else{ //We already have an existing instance
            return DatabaseDrivers.instance;
        }
    }
    
    //==================== END OF STATIC FUNCTIONS =============================

    private DatabaseDriver defaultDriver;
    
    /**
     * Appends the specified element to the end of this list. The first 
     * element will be used as default.
     * 
     * @param e element to be appended to this list
     * @return <tt>true</tt> (as specified by {@link Collection#add})
     */
    @Override
    public boolean add(DatabaseDriver e) {
        // Keep the first added Driver as default
        if(e instanceof DatabaseDriver && this.defaultDriver == null){ 
            this.defaultDriver = e;
        }
        return super.add(e);
    }
    
    /**
     * Get the default Driver to use.
     * 
     * @return Default Database Driver
     */
    public DatabaseDriver getDriver(){
        return this.defaultDriver;
    }
    
    /**
     * Find the driver with a specific ID.
     * 
     * @param driverID The driver ID as String
     * @return The DatabaseDriver or null
     */
    public DatabaseDriver find(String driverID){
        for(DatabaseDriver dd : this){
            if(dd.getDriverID().equals(driverID)){
                return dd;
            }
        }
        return null;
    }
    
}
